package com.example.innovate.controller;

/*
    管理员接口
    2021.9.13 update
*/

import com.example.innovate.bean.Result;
import com.example.innovate.domain.AchievementCompetitionDO;
import com.example.innovate.domain.CompetitionRefuseDO;
import com.example.innovate.service.AchieveCompetitionService;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.CompetitionRefuseService;
import com.example.innovate.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
@Api(tags = "管理员接口")
public class AdminController {
    @Autowired
    CompetitionRefuseService competitionRefuseService;
    @Autowired
    AchieveCompetitionService achieveCompetitionService;
    @Autowired
    AdminService adminService;

    //@UserLoginToken
    @GetMapping("/findAllRefuse")
    @ApiOperation("查看学科竞赛被拒绝理由")
    public Result findAllRefuse(@RequestParam("pageNumber") Integer pageNumber,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam("className") String className) throws Exception{
        if (pageNumber == null){
            pageNumber = 1;
        }
        List<CompetitionRefuseDO> list = competitionRefuseService.findAllByClass(className,pageNumber,pageSize);
        for(int i=0;i<list.size();i++){
            competitionRefuseService.deleteRefuse(list.get(i).getRefuseId());
        }
        return new Result(200,"success","返回成功",list);
    }

    //@UserLoginToken
    @GetMapping("/findCompetition")
    @ApiOperation("查看审核中竞赛信息")
    public Result findCompetition(@RequestParam("pageNumber") Integer pageNumber,
                                  @RequestParam("pageSize") Integer pageSize,
                                  @RequestParam("className") String className) throws Exception{
        if (pageNumber == null){
            pageNumber = 1;
        }
        List<AchievementCompetitionDO> list = achieveCompetitionService.findAllExamineByClass(className,pageNumber,pageSize);
        return new Result(200,"success","返回成功",list);


    }

    //@UserLoginToken
    @PutMapping("/updateCompetition")
    @ApiOperation("修改审核中竞赛信息")
    public Result updateCompetition(@RequestBody AchievementCompetitionDO achievementCompetitionDO) throws Exception{
        achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
        return new Result(200,"success","修改成功",achievementCompetitionDO);

    }

    //@UserLoginToken
    @PostMapping("/upload")
    @ApiOperation("修改审核中文件")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws Exception{
        AchievementCompetitionDO achievementCompetitionDO = achieveCompetitionService.findCompetitionById(id);
        String fileName = FileUtil.upload(file);
        if(achievementCompetitionDO!=null){
            if(achievementCompetitionDO.getComRemark()!=null){
                String deleteName = achievementCompetitionDO.getComRemark();
                String filePath = "/root/file/" ;//C:\\file\\
                File filePast = new File(filePath + deleteName);
                if (filePast.isFile() && filePast.exists()) {
                    filePast.delete();
                }
                achievementCompetitionDO.setComRemark(fileName);
                achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
                return new Result(200,"success","修改成功",achievementCompetitionDO);
            }
            else{
                achievementCompetitionDO.setComRemark(fileName);
                achieveCompetitionService.addCompetition(achievementCompetitionDO);
                return new Result(200,"success","修改成功",achievementCompetitionDO);
            }
        }
        else{
            return new Result(400,"error","文件不存在",null);
        }

    }





}
